<?

class BandGuestPerformersList extends db {

	function get_total() {
		$q = 'select count(ID) as total from Band_Guest_Performers';
		$r = mysql_query($q);
		if ($r) {
			$row = mysql_fetch_assoc($r);
			return $row['total'];
		} else {
			return Error::MySQL();
		}
	}
	
	function get_guest_performers() {
		$db = new db;
		$args_list = func_get_args();
		$number_to_get = (int) $args_list[0];
		$start_number = (int) $args_list[1];
		$is_active = $args_list[2];
		
		$query = "select ID from Band_Guest_Performers ";
		if (isset($is_active)) {
			$query .= "where is_active = {$is_active}";
		}
		$query .= " order by lastname asc ";
		
		if ($number_to_get && $start_number) {
			$query .= "limit $start_number, $number_to_get";
		} else if ($number_to_get) {
			$query .= "limit 0, $number_to_get";
		} else if ($start_number) {
			$query .= "limit $start_number, 10";
		} else {
			$query .= "limit 0, 10";
		}
		
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}

		$gp = array();
		while ($row = mysql_fetch_assoc($r)) {
			$gp[] = BandGuestPerformer::get($row['ID']);
		}
		return $gp;
	}
	
}

class BandGuestPerformer extends db {

	var $ID, $firstname, $lastname, $function, $description, $website, $is_active;

	function getID() {return $this->ID;}
	function getFirstName() {return $this->firstname;}
	function getLastName() {return $this->lastname;}
	function getFunction() {return $this->function;}
	function getDescription() {return $this->description;}
	function getWebsite() {return $this->website;}
	function isActive() {return $this->is_active;}
	
	function get($gpID) {
		if (is_numeric($gpID) && $gpID > 0) {
			$q = "select * from Band_Guest_Performers where ID = " . $gpID;
			$r = mysql_query($q);
			$row = mysql_fetch_assoc($r);		
			if ($row['ID']) {
				$gp = new BandGuestPerformer;
				$gp->ID = $row['ID'];
				$gp->firstname = db::sanitize_from_db($row['firstname']);
				$gp->lastname = db::sanitize_from_db($row['lastname']);
				$gp->function = db::sanitize_from_db($row['function']);
				$gp->description = db::sanitize_from_db($row['description']);
				$gp->website = db::sanitize_from_db($row['website']);
				$gp->is_active = $row['is_active'];			
				return $gp;
			}
		}
		
		return Error::create("Invalid guest performer ID.");
	}
	
	function add($postArray) {
		$db = new db;
		$e = new Error();
		
		$firstname = $db->sanitize_to_db($postArray['firstname']);
		if (!$firstname) {
			$e->add("A band member entry must contain a first name.");
		}
		
		$lastname = $db->sanitize_to_db($postArray['lastname']);
		$function = $db->sanitize_to_db($postArray['function']);
		if (!$function) {
			$e->add("A guest performer must serve a function.");
		}
			
		$description =  $db->sanitize_to_db($postArray['description']);
		$website =  $db->sanitize_to_db($postArray['website']);
		
		if ($e->hasErrors()) {
			return $e;
		}
		
		if (User::isAdmin()) {
			$r = @mysql_query("insert into Band_Guest_Performers (firstname, lastname, function, description, website, is_active) values ('$firstname','$lastname','$function','$description','$website'," . DEFAULT_ACTIVE . ")");	
			if (!$r) {
				return Error::MySQL();
			} else {
				$gp = BandGuestPerformer::get(mysql_insert_id());
				return $gp;
			}
		} else {
			return Error::create("Only an administrator may create guest performers.");
		}
	}	
	
	function update($postArray) {
		$db = new db;
		$e = new Error();
		$firstname = $db->sanitize_to_db($postArray['firstname']);
		if (!$firstname) {
			$e->add("A guest performer entry must contain a first name.");
		}
		
		$lastname = $db->sanitize_to_db($postArray['lastname']);
		$function = $db->sanitize_to_db($postArray['function']);
		if (!$function) {
			$e->add("A guest performer must serve a function.");
		}
		$description = $db->sanitize_to_db($postArray['description']);
		$website =  $db->sanitize_to_db($postArray['website']);
		
		if ($e->hasErrors()) {
			return $e;
		}
		
		if (User::isAdmin()) {
			$r = @mysql_query("update Band_Guest_Performers set firstname='$firstname', lastname='$lastname', function='$function', description='$description', website='$website' where ID = {$this->ID}");
			if (!$r) {
				return Error::MySQL();
			} else {
				return $this;
			}
		} else {
			return Error::create("Only an administrator may update guest performers.");
		}		
	}	
	
	function activate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Band_Guest_Performers set is_active = 1 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may activate guest performers.");
		}
	}
	
	function deactivate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Band_Guest_Performers set is_active = 0 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may deactivate guest performers.");
		}
	}
	
	function remove() {
		if (User::isAdmin()) {
			$r = @mysql_query("delete from Band_Guest_Performers where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may remove guest performers.");
		}
	}


}
?>
